package com.dp.service.impl3;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.JsonPage;
import com.dp.pojo.NewsVo;
import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsK16TitleToOther;

@Service
@Transactional
public class K16TitleToOtherServiceImpl extends BaseImpl {

	/**
	 * 获得所有k16跳转信息总记录数
	 * @param hashMap
	 * @return Integer
	 */
	public Integer findAllNewsEntityCount(DataMap hashMap){
		NewsK16TitleToOther news=(NewsK16TitleToOther) hashMap.get("news");
		Integer iCount = 0; 
//		try {
//			this.init();
//			StringBuffer sqlS =new StringBuffer("select count(nc.cid) from news_contenttitle nc WHERE NOT EXISTS (SELECT * FROM news_k16titletoother tc WHERE nc.cid=tc.cid)");
//			if(news!=null){
//				if(news.getContentName()!=null&& news.getContentName().toString().trim().length() > 0){
//					sqlS.append(" AND nc.contenttitle like '%"+news.getContentName().toString().trim() + "%'");
//				}
//				if (news.getCid() != null) {
//					sqlS.append(" AND nc.cid = " + news.getCid());
//				}
//			}
//			logger.info("findAllK16TitleToOtherEntityCount excute sql: " + sqlS.toString());
//			this.rs1=stmt.executeQuery(sqlS.toString());
//			if(this.rs1.next()){
//				iCount=rs1.getInt(1);
//			}
//			this.closeAll();
//		} catch (Exception e) {
//			logger.error("findAllK16TitleToOtherEntityCount fail !!!" + e.getMessage());
//			e.printStackTrace();
//		} finally {
//			this.closeAll();
//		}
		return iCount;
	}
	
	/**
	 * 获得所有k16跳转信息
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsVo>
	 */
//	public List<NewsK16TitleToOther> findAllNewsEntity(DataMap hashMap, Integer firstrow,Integer maxrow){
//		JsonPage jpage=(JsonPage) hashMap.get("jpage");
//		NewsK16TitleToOther news=(NewsK16TitleToOther) hashMap.get("news");
//		List<NewsK16TitleToOther> newsList = null; 
//		try {
////			this.init();
//			StringBuffer sqlS =new StringBuffer("select nc.cid,nc.contenttitle,nc.updatetime" +
//					" from news_contenttitle nc WHERE NOT EXISTS (SELECT * FROM news_k16titletoother tc WHERE nc.cid=tc.cid)");
//			if(news!=null){
//				if(news.getContentName() != null && news.getContentName().trim().length() > 0){
//					sqlS.append(" AND nc.contenttitle like '%" + news.getContentName().trim() + "%'");
//				}
//				if (news.getCid() != null) {
//					sqlS.append(" AND nc.cid = " + news.getCid());
//				}
//			}
//			if(jpage!=null){
//				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
//					sqlS.append(" order by "+jpage.getSidx()+" "+jpage.getSord()+"");
//				}
//			}
//			if(firstrow!=null&&maxrow!=null){
//				sqlS.append(" limit "+firstrow+","+maxrow+"");
//			}
//			logger.info("findAllNewsEntity excute sql: " + sqlS.toString());
//			ArrayList conList = this.queryALLResultSet(sqlS.toString());
//			newsList=new ArrayList<NewsK16TitleToOther>();
//			NewsK16TitleToOther newsK16TitleToOther=null;
//			SimpleDateFormat fmt_temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			if(conList.size()>0)
//			{
//				for(int i=0;i<conList.size();i++)
//				{
//					HashMap tmp = (HashMap)conList.get(i);
//					newsK16TitleToOther=new NewsK16TitleToOther();
//					newsK16TitleToOther.setCid(Integer.parseInt((String)tmp.get("cid")));
//					newsK16TitleToOther.setContentName((String)tmp.get("contenttitle"));
//					newsK16TitleToOther.setUpdatetime(fmt_temp.parse((String)tmp.get("updatetime")));
//					newsList.add(newsK16TitleToOther);
//				}
//			}
////			this.closeAll();
//		} catch (Exception e) {
//			logger.error("findAllNewsEntity fail !!!" + e.getMessage());
//			e.printStackTrace();
//		} 
//		return newsList;
//	}

	/**
	 * 获得所有k16跳转信息总记录数
	 * @param hashMap
	 * @return Integer
	 */
	public Integer findAllK16TitleToOtherEntityCount(DataMap hashMap){
		NewsK16TitleToOther news=(NewsK16TitleToOther) hashMap.get("news");
		Integer iCount = 0; 
		try {
		
			StringBuffer sqlS =new StringBuffer("select count(tc.cid) from news_k16titletoother tc,news_contenttitle nc where tc.cid=nc.cid ");
			if(news!=null){
				if(news.getContentName()!=null&& news.getContentName().toString().trim().length() > 0){
					sqlS.append(" AND nc.contenttitle like '%"+news.getContentName().toString().trim() + "%'");
				}
				if (news.getCid() != null) {
					sqlS.append(" AND tc.cid = " + news.getCid());
				}
				if (news.getCtype() != null) {
					sqlS.append(" AND tc.ctype = " + news.getCtype());
				}
				if (news.getFlag() != null) {
					sqlS.append(" AND tc.flag = " + news.getFlag());
				}
			}
			logger.info("findAllK16TitleToOtherEntityCount excute sql: " + sqlS.toString());
		
				iCount=this.getOneInt();
		} catch (Exception e) {
			logger.error("findAllK16TitleToOtherEntityCount fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return iCount;
	}
	
	/**
	 * 获得所有k16跳转信息
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsVo>
	 */
	public List<NewsK16TitleToOther> findAllK16TitleToOtherEntity(DataMap hashMap, Integer firstrow,Integer maxrow){
		JsonPage jpage=(JsonPage) hashMap.get("jpage");
		NewsK16TitleToOther news=(NewsK16TitleToOther) hashMap.get("news");
		List<NewsK16TitleToOther> newsList = null; 
		try {
		
			StringBuffer sqlS =new StringBuffer("select tc.cid,tc.ctype,tc.cparam1,tc.cparam2,tc.flag,tc.note,nc.contenttitle,nc.updatetime" +
					" from news_k16titletoother tc,news_contenttitle nc where tc.cid=nc.cid ");
			if(news!=null){
				if(news.getContentName()!=null&& news.getContentName().toString().trim().length() > 0){
					sqlS.append(" AND nc.contenttitle like '%" + news.getContentName().trim() + "%'");
				}
				if (news.getCid() != null) {
					sqlS.append(" AND tc.cid = " + news.getCid());
				}
				if (news.getCtype() != null) {
					sqlS.append(" AND tc.ctype = " + news.getCtype());
				}
				if (news.getFlag() != null) {
					sqlS.append(" AND tc.flag = " + news.getFlag());
				}
			}
			
			if(jpage!=null){
				if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
					sqlS.append(" order by "+jpage.getSidx()+" "+jpage.getSord()+"");
				}
			}
			if(firstrow!=null&&maxrow!=null){
				sqlS.append(" limit "+firstrow+","+maxrow+"");
			}
			logger.info("findAllK16TitleToOtherEntity excute dql: " + sqlS.toString());
			ArrayList conList = this.queryALLResultSet(sqlS.toString());
			newsList=new ArrayList<NewsK16TitleToOther>();
			NewsK16TitleToOther newsK16TitleToOther=null;
			SimpleDateFormat fmt_temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(conList.size()>0)
			{
				for(int i=0;i<conList.size();i++)
				{
					HashMap tmp = (HashMap)conList.get(i);
					newsK16TitleToOther=new NewsK16TitleToOther();
					newsK16TitleToOther.setCid(Integer.parseInt((String)tmp.get("cid")));
					newsK16TitleToOther.setCtype(Integer.parseInt((String)tmp.get("ctype")));
					newsK16TitleToOther.setCparam1((String)tmp.get("cparam1"));
					newsK16TitleToOther.setCparam2((String)tmp.get("cparam2"));
					newsK16TitleToOther.setFlag(Integer.parseInt((String)tmp.get("flag")));
					newsK16TitleToOther.setNote((String)tmp.get("note"));
					newsK16TitleToOther.setContentName((String)tmp.get("contenttitle"));
					newsK16TitleToOther.setUpdatetime(fmt_temp.parse((String)tmp.get("updatetime")));
					newsList.add(newsK16TitleToOther);
				}
			}
		
		} catch (Exception e) {
			logger.error("findAllK16TitleToOtherEntity fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return newsList;
	}
	
	public NewsK16TitleToOther getK16TitleToOtherByCid(String cid)
	{
		NewsK16TitleToOther tmp = null;
//		this.sql = "select tc.cid,tc.ctype,tc.cparam1,tc.cparam2, tc.flag,tc.note,nc.contenttitle,nc.updatetime" +
//					" from news_k16titletoother tc,news_contenttitle nc where tc.cid=nc.cid and tc.cid = ?";
//		try {
//			this.init();
//			this.prep = this.conn.prepareStatement(sql);
//			this.prep.setString(1, cid);
//			this.rs1 = this.prep.executeQuery();
//			if (this.rs1.next()) {
//				tmp = new NewsK16TitleToOther();
//				tmp.setCid(this.rs1.getInt(1));
//				tmp.setCtype(this.rs1.getInt(2));
//				tmp.setCparam1(this.rs1.getString(3));
//				tmp.setCparam2(this.rs1.getString(4));
//				tmp.setFlag(this.rs1.getInt(5));
//				tmp.setNote(this.rs1.getString(6));
//				tmp.setContentName(this.rs1.getString(7));
//				tmp.setUpdatetime(this.rs1.getTimestamp(8));
//			}
//		} catch (Exception e) {
//			log.error(" getK16TitleToOtherByCOntentid fail !!!" +e.getMessage());
//			e.printStackTrace();
//		}finally{
//			this.closeAll();
//		}
		return tmp;
	}
	
	public void addK16TitleToOthers(String cids, NewsK16TitleToOther news) 
	{
		String cidArr[] = cids.split(",");
		sql = "insert into news_k16titletoother (cid,ctype,cparam1,cparam2,flag,note) values (?,?,?,?,?,?)";
//		try {
//			this.init();
//			this.prep = this.conn.prepareStatement(sql);
//			for (String cid : cidArr) {
//				if (!"".equals(cid)) {
//					this.prep.setInt(1, Integer.parseInt(cid));
//					this.prep.setInt(2, news.getCtype());
//					this.prep.setString(3, news.getCparam1());
//					this.prep.setString(4, news.getCparam2());
//					this.prep.setInt(5, news.getFlag());
//					this.prep.setString(6, news.getNote());
//					this.prep.executeUpdate();
//				}
//			}
//			this.closeAll();
//		} catch (Exception e) {
//			log.error(" addK16TitleToOthers fail !!!" +e.getMessage());
//			log.error("Error Occur.When execut the sql : "+sql+" "+e);
//			e.printStackTrace();
//		}finally{
//			this.closeAll();
//		}
	}

	public boolean updateK16TitleToOther(NewsK16TitleToOther news) {
		sql = "update news_k16titletoother set ctype=?,cparam1=?,cparam2=?,flag=?,note=? where cid=?";
		return true;
//		try {
//			this.init();
//			this.prep = this.conn.prepareStatement(sql);
//			this.prep.setInt(1, news.getCtype());
//			this.prep.setString(2, news.getCparam1());
//			this.prep.setString(3, news.getCparam2());
//			this.prep.setInt(4, news.getFlag());
//			this.prep.setString(5, news.getNote());
//			this.prep.setInt(6, news.getCid());
//			this.prep.executeUpdate();
//			this.closeAll();
//			return true;
//		} catch (Exception e) {
//			log.error(" updateK16TitleToOther fail !!!" +e.getMessage());
//			e.printStackTrace();
//			return false;
//		}finally{
//			this.closeAll();
//		}
	}
	
	public boolean deleteK16TitleToOtherByCids(String cids) {
		sql = "delete from news_k16titletoother where cid in ("+cids+")";
		return true;
//		try {
//			this.init();
//			this.prep = this.conn.prepareStatement(sql);
//			this.prep.executeUpdate();
//			this.closeAll();
//			return true;
//		} catch (Exception e) {
//			log.info(" deleteK16TitleToOther fail !!!" +e.getMessage());
//			e.printStackTrace();
//			return false;
//		}finally{
//			this.closeAll();
//		}
	}
}
